﻿
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models.Common;
using Models.JsonResultModel;
using ServiceWeb.DBGateway;
using Models.WebAdmin;
using ServiceWeb.WebAdmin;
using Microsoft.EntityFrameworkCore;
using UtilsBase;
using UtilsORM;
using UtilsORM.MySqlData;
using UtilsORM.MySqlData;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using ServiceWeb.DBBase;
using NewLife.Http;
using GatewayServer.Common;

namespace GatewayServer.Controllers
{
    public class ProductLanguageAloneController : BaseMagerController
    {
        EFYihongIotContext ef = new EFYihongIotContext();


        public IActionResult list(IFormCollection FC, HttpReqModel httpRequest, iot_product reqProduct, int PageIndex = 1, int PageSize = 15)
        {
            ViewData["reqProduct"] = reqProduct;
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " ";


            if (!string.IsNullOrEmpty(httpRequest.pGuid))
            {
                sqlwhere = sqlwhere + "  and lp_p_guid = @p_guid ";
                sugarParameters.Add(new SugarParameter("p_guid", httpRequest.pGuid));
            }

            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and lp_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));
            }


            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            string sql = "select *,(SELECT GROUP_CONCAT(l_name SEPARATOR ', ')  FROM bs_language\r\ninner join bs_language_product_item_alone on lpi_l_guid=l_guid where lpi_lp_guid=lp_guid) lname  from   bs_language_product_alone  where 1=1 " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "lp_id desc", ref totalRecord);

            ViewData["DataList"] = dt;
            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码

            return View(httpRequest);
        }


        public IActionResult edit(HttpReqModel httpReqModel)
        {
            ViewBag.pGuid = httpReqModel.pGuid;
            if (httpReqModel.order == "upd")
            {
                ViewBag.edit_guid = httpReqModel.edit_guid.ToString();
            }
            else
            {
                ViewBag.edit_guid = Guid.NewGuid().ToString();
            }
            ViewBag.order = httpReqModel.order;

            return View();
        }
        public RespResultData saveAll(IFormCollection FC, HttpReqModel httpRequest, bs_language language, bs_language_product_alone config, bs_language_product_item_alone item)
        {
            config.lp_guid = httpRequest.edit_guid;
            config.lp_type = 0;
            new DBBLanguageProductAlone().saveAll(config);
            if (!string.IsNullOrEmpty(language.l_guid))
            {
                item.lpi_guid = Guid.NewGuid().ToString();
                item.lpi_l_guid = language.l_guid;
                item.lpi_lp_guid = config.lp_guid;
                new DBBLanguageProductAlone().saveItem(item);
            }
            return RespResult.Sucess();
        }

        public RespResultData getLanguages(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBBsLanguage().getLanguages());
        }
        public RespResultData getLanguagesConfigItemsForDataTable(HttpReqModel httpRequest)
        {
            DataTable dt = new DBBLanguageProductAlone().getLanguagesConfigItemsForDataTable(httpRequest.guid);
            RespResultData r = RespResult.Sucess(dt);
            return r;
        }
        public RespResultData del(IFormCollection FC, HttpReqModel httpReqModel)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            ef.bs_language_product_item_alone.Where(c => c.lpi_lp_guid == httpReqModel.guid).ExecuteDelete();
            ef.bs_language_product_alone.Where(c => c.lp_guid == httpReqModel.guid).ExecuteDelete();
            return RespResult.Sucess();
        }
        public RespResultData delitem(IFormCollection FC, HttpReqModel httpReqModel)
        {
            EFYihongIotContext ef = new EFYihongIotContext();
            ef.bs_language_product_item_alone.Where(c => c.lpi_guid == httpReqModel.guid).ExecuteDelete();
            return RespResult.Sucess();
        }
        public RespResultData getConfig(HttpReqModel httpRequest)
        {
            return RespResult.Sucess(new DBBLanguageProductAlone().getLanguagesConfig(httpRequest.guid));
        }


        public IActionResult listLanguage(IFormCollection FC, HttpReqModel httpRequest, iot_device reqDevice, iot_product reqProduct, int PageIndex = 1, int PageSize = 15, string type = "1")
        {
            ViewData["reqDevice"] = reqDevice;
            ViewData["reqProduct"] = reqProduct;
            ViewBag.d_gw_d_guid = httpRequest.gwdguid;
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            int totalRecord = 0;
            string sqlwhere = " ";

            if (!string.IsNullOrEmpty(reqProduct.p_guid))
            {
                // sqlwhere = sqlwhere + "  and c_code not in(select lp_code from bs_language_product_alone where lp_p_guid = @p_guid) ";
                sugarParameters.Add(new SugarParameter("p_guid", reqProduct.p_guid));
            }

            if (!string.IsNullOrEmpty(httpRequest.name))
            {
                sqlwhere = sqlwhere + " and c_name like CONCAT('%',@name,'%') ";
                sugarParameters.Add(new SugarParameter("name", httpRequest.name));
            }


            if (!string.IsNullOrEmpty(FC["hid_pageIndex"]))
            {
                PageIndex = int.Parse(FC["hid_pageIndex"].ToString());

            }

            string sql = "select * ,(case when lp_code is not null then 1 else 0 end ) flag,(SELECT GROUP_CONCAT(l_name SEPARATOR ', ')  FROM bs_language\r\ninner join bs_language_config_item on ci_l_guid=l_guid where ci_c_guid=c_guid) lname from   bs_language_config left join bs_language_product_alone on  lp_code=c_code and lp_p_guid = @p_guid where 1=1 " + sqlwhere;
            DataTable dt = MySqlYihongIot.getPageDataTable(PageIndex, PageSize, sql, sugarParameters, "c_id desc", ref totalRecord);
            ViewData["DataList"] = dt;
            ViewBag.PageSize = PageSize;
            ViewBag.RecordCount = totalRecord;//总条数
            ViewBag.PageCount = totalRecord % PageSize == 0 ? totalRecord / PageSize : totalRecord / PageSize + 1;//分的页数
            ViewBag.PageIndex = PageIndex;//当前页码

            return View(httpRequest);
        }



        public RespResultData addtoproduct(HttpReqModel httpRequest, string cguid, string pguid)
        {
            EFYihongIotContext eFIotBase = new EFYihongIotContext();
            bs_language_config config = eFIotBase.bs_language_config.Where(c => c.c_guid == cguid).FirstOrDefault();


            //判断是否已经存在
            List<bs_language_product_alone> getdatas = new EFYihongIotContext().bs_language_product_alone.Where(c => c.lp_p_guid == pguid && c.lp_code == config.c_code).ToList();
            foreach (bs_language_product_alone item in getdatas)
            {
                new EFYihongIotContext().bs_language_product_item_alone.Where(c => c.lpi_lp_guid == item.lp_guid).ExecuteDelete();
            }
            new EFYihongIotContext().bs_language_product_alone.Where(c => c.lp_p_guid == pguid && c.lp_code == config.c_code).ExecuteDelete();

            bs_language_product_alone product = new bs_language_product_alone();
            product.lp_p_guid = pguid;
            product.lp_add_time = DateTime.Now;
            product.lp_guid = Guid.NewGuid().ToString();
            product.lp_name = config.c_name;
            product.lp_code = config.c_code;
            product.lp_type = 1;//0独立，1公共
            eFIotBase.bs_language_product_alone.Add(product);
            eFIotBase.SaveChanges();

            List<bs_language_config_item> items = eFIotBase.bs_language_config_item.Where(c => c.ci_c_guid == cguid).ToList();

            foreach (bs_language_config_item item in items)
            {
                bs_language_product_item_alone additem = new bs_language_product_item_alone();
                additem.lpi_lp_guid = product.lp_guid;
                additem.lpi_content = item.ci_content;
                additem.lpi_addtime = DateTime.Now;
                additem.lpi_l_guid = item.ci_l_guid;
                additem.lpi_guid = Guid.NewGuid().ToString();
                eFIotBase.bs_language_product_item_alone.Add(additem);
                eFIotBase.SaveChanges();
            }

            return RespResult.Sucess();
        }
    }
}